//
//  SINATecScor/Users/androidlongs/Desktop/code/ios/application/choolsystem-ios/schoolsystem/home/controll/teacher/SINAScoreQueryViewController.xibeQueryDetailListCourseViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2018/1/8.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINATecScoreQueryDetailListCourseViewController.h"
#import "SINATecScoreQueryDetailViewController.h"
#import "SINAUiviewClickLiserner.h"
#import "SINATecManagerSignDetaileViewController.h"
#import "SINACustomModelPickView.h"

@interface SINATecScoreQueryDetailListCourseViewController ()<CustomModelPickViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;
//学年选择
@property (weak, nonatomic) IBOutlet UIView *selectClassView;
@property (weak, nonatomic) IBOutlet UILabel *showOrderTypeLabel;

@property (weak, nonatomic) IBOutlet UILabel *noDataLabel;

//学生成绩
@property(strong,nonatomic) NSMutableArray *stuScoreArray;
//排序方式
@property(strong,nonatomic) NSMutableArray *scoreOrderArray;

@property(assign,nonatomic) NSInteger orderInfo;

@end

@implementation SINATecScoreQueryDetailListCourseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:0 target:self action:@selector(dismiss)];
    //backItem.title = @"返回";
    backItem.image = [UIImage imageNamed:@"back1"];
    self.navigationItem.leftBarButtonItem = backItem;
    
    
    self.stuScoreArray=[[NSMutableArray alloc]init];
    self.scoreOrderArray=[[NSMutableArray alloc]init];
    
    for (NSInteger i=0; i<3; i++) {
        SINACommonPopModel *popModel=[[SINACommonPopModel alloc]init];
        switch (i) {
            case 0:
                 popModel.title =@"默认";
                break;
            case 1:
                popModel.title =@"成绩从高到低";
                break;
            case 2:
                popModel.title =@"成绩从低到高";
                break;
            default:
                 popModel.title =@"默认";
                break;
        }
        [self.scoreOrderArray addObject:popModel];
       
    }
    
 
    self.orderInfo=0;
    
    if ([self.pageType isEqualToString:@"成绩查询"]) {
         [self loadingSchoolYearSubScoreListNetData];
    } else {
        //点名查询
        DLog(@"点名查询 页面 %@  %@",self.schoolYearModel.schoolyear,self.schoolYearModel.term);
        [self loadingSchoolSubSignListNetData];
    }
    
   
    
}

-(void) initScoreItmeFunction{
    
      [self.contentScrollView removeAllSubviews];
    //每行列数
    NSInteger rank = 2;
    //每列间距
    CGFloat rankMargin = 0;
    //每行间距
    CGFloat rowMargin = 0;
    //Item索引 ->根据需求改变索引
    NSUInteger index = self.stuScoreArray.count;
    
    //每个Item宽高
    CGFloat W = (mainW-(rank-1)*rankMargin)/rank;
    CGFloat H = 45;
    
    for (int i = 0 ; i< index; i++) {
        SINATecCourseModel *model =self.stuScoreArray[i];
        //Item X轴
        CGFloat X = (i % rank) * (W + rankMargin);
        //Item Y轴
        NSUInteger Y = (i / rank) * (H +rowMargin);
        //Item top
        CGFloat top = 0;
        UIView *speedView = [[UIView alloc] init];
        speedView.backgroundColor = [UIColor whiteColor];
        speedView.frame = CGRectMake(X, Y+top, W, H);
        speedView.layer.borderWidth = 0.3;
        speedView.layer.borderColor = [[UIColor colorWithHexString:@"eeeeee"] CGColor];
        UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, 100, 21)];
        textLabel.text=[NSString stringWithFormat:@"%@",model.username];
        textLabel.font=[UIFont systemFontOfSize:16];
        textLabel.textColor=[UIColor colorWithHexString:@"#878787"];
        [speedView addSubview:textLabel];
        
        UILabel *scroeabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 15,W-30, 21)];
        scroeabel.text=[NSString stringWithFormat:@"%@",model.score];
        scroeabel.textColor=[UIColor colorWithHexString:@"#878787"];
        scroeabel.font=[UIFont systemFontOfSize:16];
        scroeabel.textAlignment=NSTextAlignmentRight;
        [speedView addSubview:scroeabel];
        
        if ([self.pageType isEqualToString:@"成绩查询"]) {
            textLabel.text=[NSString stringWithFormat:@"%@",model.username];
            scroeabel.text=[NSString stringWithFormat:@"%@",model.score];
            [[SINAUiviewClickLiserner alloc]viewClickFunction:speedView andWithBlick:^(UIView *view) {
//                SINATecScoreQueryDetailViewController *controller = [[SINATecScoreQueryDetailViewController alloc]initWithNibName:@"SINATecScoreQueryDetailViewController" bundle:nil];
                //[self.navigationController pushViewController:controller animated:YES];
            }];
        } else {
            //点名查询
           
            textLabel.text=[NSString stringWithFormat:@"%@",model.realname];
            scroeabel.text=[NSString stringWithFormat:@"%@/%@",model.signcnt,model.lessonperiod];
            [[SINAUiviewClickLiserner alloc]viewClickFunction:speedView andWithBlick:^(UIView *view) {
                SINATecManagerSignDetaileViewController *controller = [[SINATecManagerSignDetaileViewController alloc]initWithNibName:@"SINATecManagerSignDetaileViewController" bundle:nil];
                controller.model = self.model;
                controller.courseModel=model;
                controller.schoolYearModel = self.schoolYearModel;
                controller.subClassModel = self.subClassModel;
                [self.navigationController pushViewController:controller animated:YES];
            }];
        }
        
        
        [self.contentScrollView addSubview:speedView];
        
    }
    
    if ([self.pageType isEqualToString:@"成绩查询"]) {
        self.selectClassView.hidden=NO;
        self.contentScrollView.frame=CGRectMake(0, CGRectGetHeight(self.selectClassView.frame)+30, mainW, mainH-(CGRectGetHeight(self.selectClassView.frame)+30));
        [[SINAUiviewClickLiserner alloc]viewClickFunction:self.selectClassView andWithBlick:^(UIView *view) {
            [self showSelectSchoolYearFunction];
        }];
        
    } else {
        //点名查询
        self.contentScrollView.frame=CGRectMake(0, 0, mainW, mainH);
        self.selectClassView.hidden=YES;
    }
    
    self.contentScrollView.contentSize=CGSizeMake(mainW, index/2*H+120);
}
-(void)dismiss{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


//加载成绩信息
-(void)loadingSchoolYearSubScoreListNetData{
    
    [self startLoading];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    //用户的uuid String uuid
    
    //科目id String subjectid
    //排序 String orderInfo: 2 从低到高；  1 从高到低； 0 默认
    
    [dict setObject:[NSString stringWithFormat:@"%@",self.model.courseId] forKey:@"subjectid"];
    [dict setObject:[NSString stringWithFormat:@"%ld", (long)self.orderInfo] forKey:@"orderInfo"];
    
    //班级id String classifyid
    //[dict setObject:self.model.classifyid forKey:@"classifyid"];
    
    [self.stuScoreArray removeAllObjects];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@",baseUrl,getSubjectScoreBySubjectInfoUrl] withParamters:dict success:^(id responseData) {
        [weakSelf stopLoading];
        NSString *code = responseData[@"code"];
        
        if ([code isEqualToString:@"1000"]) {
            NSArray *result = responseData[@"content"];
            if (result==nil||result.count==0) {
                self.noDataLabel.hidden=NO;
                self.contentScrollView.hidden=YES;
            } else {
                //有班级信息
                for(NSInteger i=0;i<result.count;i++){
                    NSDictionary *dic = result[i];
                    
                    SINATecCourseModel *model = [[SINATecCourseModel alloc]initWithDict:dic];
                    [self.stuScoreArray addObject:model];
                    
                }
                self.noDataLabel.hidden=YES;
                self.contentScrollView.hidden=NO;
                [self initScoreItmeFunction];
            }
            
            
        }else{
            
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
            self.noDataLabel.hidden=NO;
            self.contentScrollView.hidden=YES;
            
        }
        
        
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
        self.noDataLabel.hidden=NO;
        self.contentScrollView.hidden=YES;
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        
    }];
}
-(void)loadingSchoolSubSignListNetData{

    [self startLoading];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    //用户的uuid String uuid
    
    //科目id String subjectid
    //排序 String orderInfo: 2 从低到高；  1 从高到低； 0 默认
    
    [dict setObject:[NSString stringWithFormat:@"%@",self.model.courseId] forKey:@"subjectid"];
    [dict setObject:[NSString stringWithFormat:@"%ld", (long)self.orderInfo] forKey:@"orderInfo"];
    
    //班级id String classifyid
    //[dict setObject:self.model.classifyid forKey:@"classifyid"];
    
    [self.stuScoreArray removeAllObjects];
    
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@",baseUrl,getSignBySubjectInfoUrl] withParamters:dict success:^(id responseData) {
        [weakSelf stopLoading];
        NSString *code = responseData[@"code"];
        
        if ([code isEqualToString:@"1000"]) {
            NSArray *result = responseData[@"content"];
            if (result==nil||result.count==0) {
                self.noDataLabel.hidden=NO;
                self.contentScrollView.hidden=YES;
            } else {
                //有班级信息
                for(NSInteger i=0;i<result.count;i++){
                    NSDictionary *dic = result[i];
                    
                    SINATecCourseModel *model = [[SINATecCourseModel alloc]initWithDict:dic];
                    [self.stuScoreArray addObject:model];
                    
                }
                self.noDataLabel.hidden=YES;
                self.contentScrollView.hidden=NO;
                [self initScoreItmeFunction];
            }
            
            
        }else{
            
            NSString *message =responseData[@"message"];
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定"
                                                                 cancleBtn:nil];
            [alertView showXLAlertView];
            self.noDataLabel.hidden=NO;
            self.contentScrollView.hidden=YES;
            
        }
        
        
        
        
    } failure:^(NSError *error, NSInteger errCode) {
        DLog(@"加载 失败  %@ ",error.debugDescription);
        [weakSelf stopLoading];
        self.noDataLabel.hidden=NO;
        self.contentScrollView.hidden=YES;
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"加载数据失败" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        
    }];
}


-(void) showSelectSchoolYearFunction{
    
    
    SINACustomModelPickView *pickerSingle = [[SINACustomModelPickView alloc]init];
    
    [pickerSingle setDataArray:self.scoreOrderArray];
    [pickerSingle setDefalutSelectRowStr:self.orderInfo];
    pickerSingle.titleLabel.text=@"选择排序";
    pickerSingle.tag=100;
    
    [pickerSingle setDelegate:self];
    
    [pickerSingle show];
    [self.view endEditing:YES];
}

#pragma mark - CustomModelPickViewDelegate method
-(void)customModelPickView:(SINACustomModelPickView *)customPickView selectedTitle:(SINACommonPopModel *)selectedModel{
    
    self.showOrderTypeLabel.text=selectedModel.title;
    if ([selectedModel.title isEqualToString:@"默认排序"]) {
        self.orderInfo=0;
    } else if ([selectedModel.title isEqualToString:@"成绩从高到低"]) {
        self.orderInfo=1;
    }else{
        self.orderInfo=2;
    }
    
    [self loadingSchoolYearSubScoreListNetData];
}
-(void)dismissCustomPickView:(SINACustomModelPickView *)customPickView{
    
}

#pragma mark - BasePickViewDelegate method
- (void)dismissBasePickView:(SINABasePickView *)customPickView
{
    
}

@end
